package com.slz3.business.module.memberManagement.memRaffleTicket.service;

import com.slz3.business.frame.enums.ValidFlag;
import com.slz3.business.frame.module.Repositorys;
import com.slz3.business.frame.module.Services;
import com.slz3.business.frame.steward.PageableSteward;
import com.slz3.business.module.memberManagement.memRaffleTicket.domain.MemRaffleTicket;
import com.slz3.business.module.memberManagement.memRaffleTicket.domain.MemRaffleTicketFactory;
import com.slz3.business.module.memberManagement.memRaffleTicket.steward.MemRaffleTicketSpecification;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.util.MultiValueMap;

import java.util.*;

@Slf4j
@Service
public class MemRaffleTicketServiceImpl implements MemRaffleTicketService {
    @Override
    public MemRaffleTicket save(MemRaffleTicket memRaffleTicket) {
        return Repositorys.memRaffleTicket.save(MemRaffleTicketFactory.initBaseFields(memRaffleTicket));
    }
    @Override
    public Page<MemRaffleTicket> page(MultiValueMap<String, String> params) {
        Page<MemRaffleTicket> memRaffleTicketPage = Repositorys.memRaffleTicket.findAll(
                MemRaffleTicketSpecification.findJoin(params), PageableSteward.extract(params)
        );
        return memRaffleTicketPage;
    }


    @Override
    public MemRaffleTicket modify(MemRaffleTicket memRaffleTicketNew) {
        return Repositorys.memRaffleTicket.saveAndFlush(MemRaffleTicketFactory.initBaseFields(memRaffleTicketNew));
    }

    @Override
    public Optional<MemRaffleTicket> single(UUID uuid) {
        return Repositorys.memRaffleTicket.findById(uuid);
    }

    @Override
    public List<MemRaffleTicket> findAllByUuids(Iterable<UUID> uuids) {
        return Repositorys.memRaffleTicket.findAllById(uuids);
    }

    @Override
    public List<MemRaffleTicket> delete(List<MemRaffleTicket> memRaffleTicketList) {
        memRaffleTicketList.stream().forEach(item -> {
            item.setValidFlag(ValidFlag.DEL);
            MemRaffleTicketFactory.initBaseFields(item);
        });
        return Services.memRaffleTicketTransactional.batchModify(memRaffleTicketList);
    }

}
